﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace Instrument
{
    public class C34970AConfiger
    {
        public C34970AConfiger(Production production)
        {
            this.ChannelSum = production.ResistanceCount;
            ChannelNumGroup = ConfigurationManager.AppSettings["ChannelString"];
        }
        private int channelSum;
        private string channelNumGroup;
        /// <summary>
        /// 通道总数
        /// </summary>
        public int ChannelSum
        {
            get
            {
                return channelSum;
            }
            set
            {
                channelSum = value;
            }
        }
        /// <summary>
        /// 当前使用的多路测试模块地址
        /// </summary>
        public MuxModel CurrentMuxModel { get; set; } = MuxModel.Mux1;
        /// <summary>
        /// 通道列表
        /// </summary>
        public string ChannelNumGroup
        {
            get
            {
                return channelNumGroup;
            }
            set
            {
                channelNumGroup = value;
                ModifyAppConfig(channelNumGroup);
            }
        }
        /// <summary>
        /// 根据通道数量启用默认通道
        /// </summary>
        /// <param name="Num"></param>
        /// <returns></returns>
        public string SetDefaultChannel(int count)
        {
            if (count > 20 || count < 1)
                throw new ArgumentException("参数无效");
            ChannelSum = count;
            StringBuilder stringBuilder = new StringBuilder("101");
            for(int i = 2;i <= count; i++)
            {
                stringBuilder.Append("," + ((int)CurrentMuxModel + i).ToString());
            }
            ModifyAppConfig(stringBuilder.ToString());
            return stringBuilder.ToString();
        }
        /// <summary>
        /// 修改对应配置文件内容
        /// </summary>
        /// <param name="s"></param>
        private void ModifyAppConfig(string s)
        {
            Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            configuration.AppSettings.Settings["ChannelString"].Value = s;
            configuration.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("AppSettings");
        }
    }
}
